/**
 * Created by yumeng on 2017/11/26.
 */
define([
  './themes/lang/' + window.YUFP_SYS_CONFIG.language + '/pages/example/template/exampleEdit.js'
], function (require, exports) {
  // page加载完成后调用ready方法
  exports.ready = function (hashCode, data, cite) {
    yufp.lookup.reg('CRUD_TYPE,NATIONALITY,PUBLISH_STATUS');
    var vm = yufp.custom.vue({
      el: '#exampleEdit',
      data: function () {
        var me = this;
        return {
          queryFields: [
            {placeholder: this.$t('exampleEdit.bt'), field: 'title', type: 'input'},
            {placeholder: this.$t('exampleEdit.sj'), field: 'create_at', type: 'date'},
            {placeholder: this.$t('exampleEdit.lx'), field: 'type', type: 'select', dataCode: 'NATIONALITY' }
          ],
          tableColumns: [
            { label: this.$t('exampleEdit.bm'), prop: 'id', width: 110 },
            { label: this.$t('exampleEdit.mc'), prop: 'title', width: 200, sortable: true, resizable: true },
            { label: this.$t('exampleEdit.lx'), prop: 'type', width: 110, dataCode: 'NATIONALITY' },
            { label: this.$t('exampleEdit.zz'), prop: 'author', width: 110 },
            { label: this.$t('exampleEdit.zt'), prop: 'status', width: 120, dataCode: 'PUBLISH_STATUS' },
            { label: this.$t('exampleEdit.sj'), prop: 'create_at', width: 110 }
          ],

          updateFields: [{
            columnCount: 2,
            fields: [
              { field: 'title', label: this.$t('exampleEdit.mc') },
              { field: 'create_at', label: this.$t('exampleEdit.sj'), type: 'date' },
              { field: 'author', label: this.$t('exampleEdit.zz') },
              { field: 'auditor', label: this.$t('exampleEdit.shr') },
              { field: 'importance', label: this.$t('exampleEdit.zyx') },
              { field: 'pageviews', label: this.$t('exampleEdit.yds') },
              { field: 'OPEN_FLAG', label: this.$t('exampleEdit.yw') },
              { field: 'MEASURE', label: this.$t('exampleEdit.yw') },
              { field: 'LOCK', label: this.$t('exampleEdit.sd') },
              { field: 'type', label: this.$t('exampleEdit.lx'), type: 'select', dataCode: 'NATIONALITY' },
              { field: 'status', label: this.$t('exampleEdit.zt'), type: 'select', dataCode: 'PUBLISH_STATUS' }
            ]
          }, {
            columnCount: 1,
            fields: [
              { field: 'remark', label: this.$t('exampleEdit.dp'), type: 'textarea', rows: 3 }
            ]
          }],
          updateButtons: [
            {label: this.$t('exampleEdit.bc'),
              type: 'primary',
              icon: 'check',
              hidden: true,
              click: function (model) {
                me.dialogVisible = false;
                me.$msgbox.alert(model, me.$t('exampleEdit.ts'));
              // 请调用服务进行后台保存
              }}
          ],
          height: yufp.frame.size().height - 103,
          grpFormHeight: yufp.frame.size().height - 103 - 127 + 'px',
          formDisabled: false,
          viewType: 'DETAIL',
          viewTitle: yufp.lookup.find('CRUD_TYPE', false)
        };
      },
      methods: {
        switchStatus: function (viewType, editable) {
          this.viewType = viewType;
          this.formDisabled = !editable;
          this.updateButtons[0].hidden = !editable;
        },
        addFn: function () {
          var me = this;
          me.switchStatus('ADD', true);
          me.$nextTick(function () {
            me.$refs.myform.resetFields();
          });
        },
        modifyFn: function () {
          if (this.$refs.mytable.selections.length < 1) {
            this.$message({message: this.$t('exampleEdit.qxxzytjl'), type: 'warning'});
            return;
          }
          this.switchStatus('EDIT', true);
          this.$nextTick(function () {
            yufp.extend(this.$refs.myform.formModel, this.$refs.mytable.selections[0]);
          });
        },
        infoFn: function () {
          if (this.$refs.mytable.selections.length < 1) {
            this.$message({message: this.$t('exampleEdit.qxxzytjl'), type: 'warning'});
            return;
          }
          this.switchStatus('DETAIL', false);
          this.$nextTick(function () {
            yufp.extend(this.$refs.myform.formModel, this.$refs.mytable.selections[0]);
          });
        },
        rowClickFn: function (row) {
          this.infoFn();
        }
      }
    });
  };
});
